const constant = require("../../util/constant.js");
const util = require('../../util/util.js');
var app = getApp();
Page({
  data: {
    color: constant.color,
    bakurl: '',
    userid:'',
    sysid: '',
    isremember:''
    //jcwid: 0
    //is_dialog: false,
    //link_name: '',
    //link_phone: '',
    //cnote: ''
    //delivery_is_default: false
  },
  checkboxChange: function (e) {
    //console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    var isck=e.detail.value;
    this.setData({
        isremember:isck
    })
  },
  onUnload: function () {

  },
  onLoad: function (option) {
    
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  },
  handlDialogOpen: function () {
    this.setData({
      //is_dialog: true
    });
  },
  handlDialogCancel: function () {
    this.setData({
      //is_dialog: false
    });
  },
  handlDialogOK: function () {


    this.setData({
      //delivery_province: delivery_province,
      //delivery_city: delivery_city,
      //delivery_area: delivery_area,
      //is_dialog: false
    });
  },
  handPickerChange: function (event) {

  },
  handleFind: function (delivery_id) {

  },
  handleSubmit: function (event) {

    //var sid = this.data.sysid;
    //console.log(sid)
    var link_pass = event.detail.value.dsrhm;
    var link_phone = event.detail.value.dsrname;

    if (link_phone == '') {
      util.showFailToast({
        title: '请输入登录账号'
      });

      return;
    }
    /* 
    else {
      if (!util.isPhone(link_phone)) {
        util.showFailToast({
          title: '手机格式不对'
        });

        return;
      }
    }
    */

    if (link_pass == '') {
      util.showFailToast({
        title: '请输入密码'
      });

      return;
    }

    //console.log(link_pass+"|"+link_phone);
    //var that = this;
    wx.request({
      url: constant.host+'/appserv/login.ashx',
      data: {
        p: link_pass,
        u: link_phone,
        k: constant.wkey
      },
      method: 'GET',
      header: {
        'Accept': 'application/json'
      },
      success: function (res) {
        //console.log(res);
          //console.log(res.data.length)
        if (res.data.length>0) {
            var corpid=res.data[0].corpid;
            //console.log(corpid);
            if(corpid>0)
            {
                wx.request({
                    url: constant.host+'/appserv/getcorp.ashx',
                    data: {
                      c: corpid,
                      k: constant.wkey
                    },
                    method: 'GET',
                    header: {
                      'Accept': 'application/json'
                    },
                    success: function (ress) {
                        //console.log(res.data.length)
                      if (ress.data.length>0) {
                          
                              wx.setStorageSync('cname', ress.data[0].CCorpName);
                              wx.setStorageSync('cno', ress.data[0].CCorpNO);
                              wx.setStorageSync('ccity', ress.data[0].CCorpCity);
                              wx.setStorageSync('caddr', ress.data[0].CCorpAddress);
                              wx.setStorageSync('clink', ress.data[0].CCorpLink);
                              wx.setStorageSync('cbank', ress.data[0].CCorpBank);
                              wx.setStorageSync('bankno', ress.data[0].CBankNO);
                          
                      }
                      else {
                        util.showFailToast({
                          title: '无登录权限'
                        });
                  
                        return;
                      }
                    },
                  });
            }
            else
            {
                util.showFailToast({
                    title: '用户名或密码错误'
                  });
            
                  return;
            }

            if(link_phone=='8888')
            {
                wx.setStorageSync('userid', res.data[0].UserID);
                wx.setStorageSync('uname', res.data[0].UserName);
                wx.setStorageSync('udept', res.data[0].Dep);
                wx.redirectTo({
                    url: '/pages/cw/index',
                  })
            }
            else
            {
                wx.setStorageSync('userid', res.data[0].UserID);
                wx.setStorageSync('uname', res.data[0].Name);
                wx.setStorageSync('udept', res.data[0].DeptName);
                wx.setStorageSync('uzfzbh', res.data[0].ZFZBH);
                wx.redirectTo({
                    url: '/pages/index/index',
                  })
            } 
        }
        else {
          util.showFailToast({
            title: '用户名或密码错误'
          });
    
          return;
        }
      },
    });

  }
});